package top.maof.book.result;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@ApiModel("响应对象")
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {

    @ApiModelProperty(value = "响应码(兼容ApiResponse)")
    private int code;

    // 状态码 200
    private Integer status;

    // 消息
    private String message;

    @ApiModelProperty(value = "异常消息(兼容ApiResponse)")
    private String exception;


    // 时间戳
    @JsonFormat(pattern = "yyyy-MM-dd hh:MM:ss")
    private Date timestamp = new Date();

    // 数据
    private T data;

    public Result(Integer status, String message) {
        code = status;
        this.status = status;
        this.message = message;
    }

    public Result(Integer status, String message, T data) {
        code = status;
        this.status = status;
        this.data = data;
        this.message = message;
    }

    public static <E> Result response(Integer status, String message, E data) {
        return new Result<E>(status, message, data);
    }

    public static Result response(Integer status, String message) {
        return new Result(status, message);
    }

    public static <E> Result success(E data) {
        return new Result<E>(200, "成功", data);
    }

    public static Result success() {
        return new Result(200, "成功");
    }

    public static Result response_400() {
        return new Result(400, "请求参数有误");
    }

    // 没有登录
    public static Result response_401() {
        return new Result(401, "当前请求需要被验证身份");
    }

    public static Result response_404() {
        return new Result(404, "请求不到资源");
    }

    public static Result response_403() {
        return new Result(403, "服务器拒绝执行当前请求");
    }

    public static Result response_423() {
        return new Result(423, "当前资源被锁定");
    }

    public static Result response_500() {
        return new Result(500, "服务器错误");
    }

    public static Result response_501() {
        return new Result(501, "服务器不支持当前请求");
    }
}
